package mapper;

import server.HttpServlet;

import java.util.ArrayList;
import java.util.List;

public class Host implements Mapper{
    private String host;

    private String port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public List<Context> getContexts() {
        return contexts;
    }

    public void setContexts(List<Context> contexts) {
        this.contexts = contexts;
    }

    private List<Context> contexts = new ArrayList<>();

    public HttpServlet getServletByUrl(String url){
        String[] split = url.split("/");
        if(split.length == 3){
            String context = split[1];
            String wrapper = split[2];

            for(Context c : this.contexts){
                if(c.getContext().equals(context)){
                    List<Wrapper> wrappers = c.getWrappers();
                    for(Wrapper w : wrappers){
                        if(w.getWrapper().equals(wrapper)){
                            return w.getServlet();
                        }
                    }
                }
            }
        }


        return null;
    }
}
